import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

/// 输入框TextField封装
class InputField extends StatelessWidget {

  final TextInputType keyboardType;
  final String hintText;
  final TextEditingController? controller;
  final TextStyle? style;
  final EdgeInsets? contentPadding;
  final int maxLines;
  final int? maxLength;
  final List<TextInputFormatter>? inputFormatter;
  final FocusNode? focusNode;
  final bool autofocus;
  final Function? onChange;
  final bool obscureText;
  final EdgeInsets? padding;
  final double? minHeight;

  /// 头部widget
  final Widget? prefix;
  /// 尾部widget
  final Widget? suffix;

  /// decoration
  final Color backgroundColor;
  final double borderRadius;
  final BoxBorder? border;
  final TextAlign? textAlign;

  const InputField({
    super.key,
    this.keyboardType = TextInputType.text,
    this.hintText = "",
    this.controller,
    this.style,
    this.contentPadding,
    this.maxLines = 1,
    this.inputFormatter,
    this.focusNode,
    this.maxLength,
    this.autofocus = false,
    this.onChange,
    this.prefix,
    this.suffix,
    this.backgroundColor = Colors.white,
    this.border,
    this.borderRadius = 0,
    this.obscureText = false,
    this.padding,
    this.textAlign,
    this.minHeight
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: padding ?? EdgeInsets.symmetric(vertical: 10, horizontal: 15),
      constraints: BoxConstraints(
          minHeight: minHeight ?? 48
      ),
      decoration: BoxDecoration(
        color: backgroundColor,
        borderRadius: BorderRadius.circular(borderRadius),
        border: border,
      ),
      child: Row(
        children: [
          if (prefix != null) prefix!,
          Expanded(
            child: TextField(
              controller: controller,
              keyboardType: keyboardType,
              inputFormatters: inputFormatter,
              focusNode: focusNode,
              autofocus: autofocus,
              obscureText: obscureText,
              textAlign: textAlign ?? TextAlign.start,
              decoration: InputDecoration(
                  hintText: hintText,
                  hintStyle: const TextStyle(fontSize: 13, color: Color(0xFFBCBCBC)),
                  contentPadding: contentPadding ?? const EdgeInsets.only(left: 0),
                  border: InputBorder.none,
                  filled: true,
                  fillColor: Colors.transparent,
                  isDense: true,
                  counterText: ""),
              maxLines: maxLines,
              maxLength: maxLength,
              style: style ?? const TextStyle(fontSize: 14, color: Color(0xFF484E76)),
              onChanged: (String input) {
                onChange?.call(input);
              },
            ),
          ),
          if (suffix != null) suffix!
        ],
      ),
    );
  }
}
